﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraController : MonoBehaviour
{
    public Camera main;
    public Transform target;
    public int MouseWheelSensitivity = 1;
    public int MouseZoomMin = 1;
    public int MouseZoomMax = 5;
    public float normalDistance = 3;

    private Vector3 normalized;

    public float xSpeed = 250.0f;
    public float ySpeed = 120.0f;

    public int yMinLimit = -20;
    public int yMaxLimit = 80;

    private float x = 0.0f;
    private float y = 0.0f;

    private Vector3 screenPoint;
    private Vector3 offset;

    private Quaternion rotation = Quaternion.Euler(new Vector3(30f, 0f, 0f));
    private Vector3 CameraTarget;
    void Start()
    {

        CameraTarget = target.position;

        float z = target.transform.position.z - normalDistance;
        transform.position = rotation * new Vector3(transform.position.x, transform.position.y, z);

        transform.LookAt(target);

        var angles = transform.eulerAngles;
        x = angles.y;
        y = angles.x;
    }

    private void ResetPos()
    {

    }

    public void SetRotate(bool canRoot)
    {
        this.CanRotate = canRoot;
    }


    bool CanRotate = false;
    void Update()
    {
        if (CanRotate)
        {
            if (Input.GetMouseButton(1) || Input.GetMouseButton(0))
            {
                x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
                y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;

                y = ClampAngle(y, yMinLimit, yMaxLimit);

                var rotation = Quaternion.Euler(y, x, 0);
                var position = rotation * new Vector3(0.0f, 0.0f, -normalDistance) + CameraTarget;

                transform.rotation = rotation;
                transform.position = position;
            }
            else if (Input.GetAxis("Mouse ScrollWheel") != 0)
            {
                var mouseScrWheel = Input.GetAxis("Mouse ScrollWheel");
                if (mouseScrWheel < 0)
                {
                    //Debug.Log(mouseScrWheel);
                    if (main.fieldOfView <= 100)
                        main.fieldOfView += 2;
                    if (main.orthographicSize <= 20)
                        main.orthographicSize += 0.5F;
                }
                //Zoom in
                if (mouseScrWheel > 0)
                {
                    //Debug.Log(mouseScrWheel);
                    if (main.fieldOfView > 2)
                        main.fieldOfView -= 2;
                    if (main.orthographicSize >= 1)
                        main.orthographicSize -= 0.5F;
                }
            }
            else if (Input.GetMouseButton(2))
            {
                y = Input.GetAxis("Mouse Y") * 0.02f;
                x = Input.GetAxis("Mouse X") * 0.02f;
                if ( Mathf.Abs( y) >=  Mathf.Abs( x))
                {
                    var position = new Vector3(target.localPosition.x, target.localPosition.y + y, target.localPosition.z);
                    target.transform.localPosition = position;
                }
                else
                {
                    var position = new Vector3(target.localPosition.x +x, target.localPosition.y , target.localPosition.z);
                    target.transform.localPosition = position;
                }
            }

            transform.LookAt(CameraTarget);
        }
    }

    static float ClampAngle(float angle, float min, float max)
    {
        if (angle < -360)
            angle += 360;
        if (angle > 360)
            angle -= 360;
        return Mathf.Clamp(angle, min, max);
    }
}
